﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Reflection;
using System.Text;

namespace DavidDB.ADO
{
    public static class Entity
    {
        /// <summary>
        /// 一对一
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="columeName"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public static dynamic Include(this object obj, string columeName, DBManager db)
        {
            PropertyInfo[] properties = obj.GetType().GetProperties();
            int id = 0;
            foreach (PropertyInfo p in properties)
            {
                if (p.Name.Equals(columeName + "Id"))
                {
                    id = Convert.ToInt32(p.GetValue(obj, null));
                }
                if (p.Name.Equals(columeName))//一对一
                {
                    var t = Activator.CreateInstance(p.PropertyType);
                    PropertyInfo[] myPropertyInfo = t.GetType().GetProperties();

                    myPropertyInfo[0].SetValue(t, id, null);

                    t = db.GetModel(t);
                    p.SetValue(obj, t, null);
                }
            }
            return obj;
        }

        ///// <summary>
        ///// 一对一
        ///// </summary>
        ///// <param name="obj"></param>
        ///// <param name="columeName"></param>
        ///// <param name="db"></param>
        ///// <returns></returns>
        //public static dynamic Include(this object obj, PropertyInfo columeName, DBManager db)
        //{
        //    PropertyInfo[] properties = obj.GetType().GetProperties();
        //    int id = 0;
        //    string _columeName = columeName.Name;
        //    foreach (PropertyInfo p in properties)
        //    {
        //        if (p.Name.Equals(_columeName + "Id"))
        //        {
        //            id = Convert.ToInt32(p.GetValue(obj, null));
        //        }
        //        if (p.Name.Equals(_columeName))//一对一
        //        {
        //            var t = Activator.CreateInstance(p.PropertyType);
        //            PropertyInfo[] myPropertyInfo = t.GetType().GetProperties();

        //            myPropertyInfo[0].SetValue(t, id, null);

        //            t = db.GetModel(t);
        //            p.SetValue(obj, t, null);
        //        }
        //    }
        //    return obj;
        //}
        ///// <summary>
        ///// 一对多
        ///// </summary>
        ///// <param name="obj"></param>
        ///// <param name="columeName"></param>
        ///// <param name="db"></param>
        ///// <returns></returns>
        //public static dynamic IncludeCollection(this object obj, string columeName, DBManager db)
        //{
        //    PropertyInfo[] properties = obj.GetType().GetProperties();
        //    int id = 0;
        //    foreach (PropertyInfo p in properties)
        //    {
        //        ArrayList list = new ArrayList();
        //        if (p.Name.Equals(columeName) && p.PropertyType.ToString().Equals(""))//一对一
        //        {
        //            //var t = Activator.CreateInstance(p.PropertyType);

        //            ////   List<> lists = new List<p.PropertyType>();

        //            //PropertyInfo[] myPropertyInfo = t.GetType().GetProperties();

        //            //myPropertyInfo[0].SetValue(t, id, null);

        //            //t = db.GetModel(t);
        //            //p.SetValue(obj, t, null);
        //        }
        //    }
        //    return obj;
        //}
    }
}
